﻿using NB.MGJ.Model.AntiFake;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NB.MGJ.Service.AntiFake;
using System.IO;
using NB.MGJ.Model;
namespace NB.MGJ.Web.Controllers
{
    public partial class AntiFakeController
    {

     
        public AntiFake_PageCreateService antifake_pagecreateService = new AntiFake_PageCreateService();



       






        #region  视图

        //微页面创建   选择模版页面
        public ActionResult  CreatePageSelectTemplet()
        {
            return View();
        }


        //微页面 配置页面
        public ActionResult CreateAntiFakePage2(int Id = 0,int TID=0)
        {
            var pageModel = new AntiFake_PageCreate();
            if (Id == 0)
            {
                pageModel = antifake_pagecreateService.CreatePageModel(LogOnUser.EnterpriseId,TID);
                var result = antifake_pagecreateService.PageCreateAddService(pageModel);
                pageModel.ID = result;
                _AntiFakeButtonService.CreateButtonListTempletAndBindPage(result,TID);

            }
            else
            {
                pageModel = antifake_pagecreateService.GetModelByID(Id);
            }

            ViewBag.pageModel = pageModel;
            ViewBag.ActionType=Id==0?0:1;
            return View();
        }




       /// <summary>
       /// 按钮编辑页面
       /// </summary>
       /// <param name="buttonId">按钮ID</param>
       /// <param name="moduleId">按钮所属模块ID</param>
       /// <returns></returns>
        public ActionResult ButtonEdit(int buttonId,int moduleId)
        {
            ViewBag.buttonId = buttonId;
            ViewBag.moduleId = moduleId;
            return View();
        }




        /// <summary>
        /// 微页面管理列表页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AntiPageConfigList()
        {
            return View();
        }


        #endregion 




        #region  基础操作


        public ActionResult SetDefault(int Id)
        {

            var result = antifake_pagecreateService.SetDefault(Id);

            return Json(result);
        }





        public ActionResult AntiPageDelete(int Id)
        {
            var result = antifake_pagecreateService.PageCreateDelete(Id);
            return Json("1");
        }





        public JsonResult AntiPageList(string search, int pageIndex = 1, int pageSize = 10)
        {

            PagerModel pm = new PagerModel();
            pm.Page = pageIndex;
            pm.PageSize = pageSize;
            pm.StrCondition = " 1=1 ";
            var model = antifake_pagecreateService.GetPageListByEid(ref pm, LogOnUser.EnterpriseId);
            return Json(model);
        }



        #endregion 




        #region 局部更新

        [HttpPost]
        public ActionResult UpdateLunBoTu(int TimeOptions, string imgListJson, int Id = 0)
        {
            var result = antifake_pagecreateService.UpdateLunBoTu(TimeOptions,imgListJson,Id);
            return Json("0");
        }


        public ActionResult CarouselFigure(int TimeOptions, string imgListJson, int Id = 0)
        {
            var result = antifake_pagecreateService.UpdateLunBoTu(TimeOptions, imgListJson, Id);
            return Json(result);
        }

        //[HttpPost]
        //public ActionResult DragAndCarouselFigureSave(int TimeOptions, string imgListJson,int picShowType,DragModel dragModel, int Id = 0)
        //{






        //    return Json("0");
        //}


        [HttpPost]
        public ActionResult DragAndCarouselFigureSave(GroupImg model)
        {
            var result = antifake_pagecreateService.UpdateGroupImg(model);
            return Json(result);
        }





        [HttpPost]
        public ActionResult UpdateBackGroundImg(string Bimg, int Id = 1)
        {

            var result = antifake_pagecreateService.UpdateBackGroundImg(Bimg, Id);
            return Json(result);
        }
        public ActionResult UpdateTitle(string title, int Id)
        {

            var result = antifake_pagecreateService.UpdateTitle(title, Id);
            return Json(result);
        }


        [ValidateInput(false)]
        public ActionResult RichTextSave(string MainContent, int Id)
        {
            var result = antifake_pagecreateService.RichTextSave(MainContent, Id);
            return Json(result);
        }












        #endregion 


 










        #region 按钮相关 

        public ActionResult GetButtonById(int buttonId)
        {
            var model = _AntiFakeButtonService.GetModel(buttonId);
            return Json(model);
        }


        public ActionResult ButtonSave(L.EntityModel.Antifake_Button model)
        {
            var result = 0;
            if (model.ButtonId == 0)
            {
                result = _AntiFakeButtonService.Add(model);
            }
            else
            {
                result = _AntiFakeButtonService.Update(model);
            }
            return Json(result);
        }

        public ActionResult ButtonDelete(int buttonId)
        {
            var result = _AntiFakeButtonService.Del(buttonId);
            return Json(result);
        }



        public ActionResult GetButtonList(int moduleId)
        {
            var list = _AntiFakeButtonService.GetListByModuleId(moduleId);
            return Json(list);
        }





        #endregion 













  
        #region   未知代码

        public ActionResult PageCreate(HttpPostedFileBase file, AntiFake_PageCreate model)
        {
            int result = antifake_pagecreateService.GetCreatePageId(LogOnUser.EnterpriseId);
            if (file != null)
            {
                string ex = Path.GetExtension(file.FileName).ToLower();
                string fName = model.EnterpriseId + ex;
                string savePath = Server.MapPath(AppConst.GenCodeLableImg + model.EnterpriseId + "/");
                if (!Directory.Exists(savePath))
                {
                    Directory.CreateDirectory(savePath);
                }
                savePath = savePath + fName;
                file.SaveAs(savePath);
                model.BackgroundImg = AppConst.GenCodeLableImg + model.EnterpriseId + "/" + fName;
            }

            if (result == 0)
            {
                result = antifake_pagecreateService.PageCreateAddService(model);

            }
            else
            {

                result = antifake_pagecreateService.PageCreateUpdateService(model);
            }
            return Json(result);
        }



        public JsonResult GetCodeBySearchList(string code, int EnterpriseId)
        {
            var result = antifake_pagecreateService.GetCodeBySearchService(code, EnterpriseId);
            return Json(new
            {
                result
            }, JsonRequestBehavior.AllowGet);
        }





        #endregion




        #region 已替换 待删除

        public ActionResult CreateAntiFakePage()
        {
            return View();
        }

        #endregion





    }
}